package tmhprediction.eval;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.HashMap;

import tmhprediction.main.TMHResultMap;

/***
 * Die Klasse erzeugt den final output in dem format:
 * proteinA sol -3
 * ----------
 * proteinA_0 1 1 0.6
 * proteinA_1 0 1 0.6
 * ==========
 * proteinB imp 13
 * ----------
 * proteinB_0 0 0 0.1
 * proteinB_1 1 1 0.9
 * ==========
 * ...
 * @author steckl
 *
 */
public class TMHFinalEvaluator {
	
	/***
	 * erzeugt die finale ausgabe (siehe klassenbeschreibung)
	 * 
	 * @param resmap1 result von svm1 (key = proteinname, value[0] = observed, value[1] = predicted, value[2] = durchschnitt von confSum-cutoff
	 * @param resmap2 result von svm2 (key = proteinname_position, value[0] = observed, value[1] = predicted, value[2] = confidence von svm2
	 */
	public static void printFinalOutput(TMHResultMap svm1res, TMHResultMap svm2res, String fileName) {
		int pos = 0;
		
		//String fileName = "c:/users/steckl/TUM/pp/testFile.txt";
		
		try{
			// Create file 
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter out = new BufferedWriter(fstream);
		
			double obs = 0;
			double pred = 0;
			double conf = 0;
		    String resName = "";
			
			for(String sProteinName : svm1res.keySet()) {
				pos = 0;
				
				obs = svm1res.getObserved(sProteinName);
				pred = svm1res.getPredicted(sProteinName);
				conf = svm1res.getConfidence(sProteinName);
				
				out.write(sProteinName + " pred: "+pred+"; conf:"+ conf+"; obs: " +obs+";\n");
				out.write("----------------\n");
				out.write("name \t pred \t conf \t obs \n");
				while(svm2res.containsKey(sProteinName +"_"+pos)) {
					resName = sProteinName +"_"+pos;
					obs = svm2res.getObserved(resName);
					pred = svm2res.getPredicted(resName);
					conf = svm2res.getConfidence(resName);
					
					out.write(resName + "\t "+pred+"\t"+ conf+"\t " +obs+"\n");
					
					pos++;
				}
				out.write("=================\n");
			}
			out.close();
		}catch (Exception e){//Catch exception if any
			  System.err.println("Error when writing final evaluation: " + e.getMessage());
			  e.printStackTrace();
		}
		
	}
	
	/*
	 * 
	 * Do not print the observerd column and also print the amino acid sequence instead of the internal proteinname_pos string
	 */
	public static void printFinalOutputNoObs(TMHResultMap svm1res, TMHResultMap svm2res, String fileName, HashMap<Integer, String> numberToNameMap, HashMap<Integer, String> numberToSequenceMap) {
		int pos = 0;
		
		//String fileName = "c:/users/steckl/TUM/pp/testFile.txt";
		
		try{
			// Create file 
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter out = new BufferedWriter(fstream);
		
//			double obs = 0;
			double pred = 0;
			double conf = 0;
		    String resName = "";
			
			for(String sProteinName : svm1res.keySet()) {
				pos = 0;
				
//				obs = svm1res.getObserved(sProteinName);
				pred = svm1res.getPredicted(sProteinName);
				conf = svm1res.getConfidence(sProteinName);
				
				out.write(numberToNameMap.get(Integer.valueOf(sProteinName)) + "\n" + "Classified as: " +
				((pred == 1.0) ? "Transmembrane" : "Soluble") +
				", Probability(Transmembrane): "+
				conf
				+ "\n");

				out.flush();
				out.write("----------------\n");
				out.write("Residue\tState\tProbability(H)\n");
				while(svm2res.containsKey(sProteinName +"_"+pos)) {
					resName = sProteinName +"_"+pos;
					
					if(!numberToSequenceMap.containsKey(Integer.valueOf(sProteinName)))
					{
						System.out.println("Number to sequence map does not contain key: " + sProteinName);
					}
					
					String tempResName = String.valueOf(numberToSequenceMap.get(Integer.valueOf(sProteinName)).charAt(pos));
					
//					obs = svm2res.getObserved(resName);
					pred = svm2res.getPredicted(resName);
					conf = svm2res.getConfidence(resName);
					
					out.write(tempResName + "\t "
					+((pred == 0.0) ? "-" : "H")
					+"\t"+ conf+"\t "+"\n");
					
					pos++;
				}
				out.write("=================\n");
			}
			out.close();
		}catch (Exception e){//Catch exception if any
			  System.err.println("Error when writing final evaluation: " + e.getMessage());
			  e.printStackTrace();
		}
		
	}
}
